﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_2203.Common.Domain;
using Traceability_2203.Common.Dto;
using Traceability_2203.Common.Infrastructure.interFaces;
using Traceability_2203.Enum;

namespace Traceability_2203.Api.Application.Commands.USers
{
    public class UserupdateHander : IRequestHandler<UserupdateCommand, Reult>
    {





        private readonly IBaseRepository<Users> _user;

        public UserupdateHander(IBaseRepository<Users> user)
        {
            _user = user;
        }

        public async Task<Reult> Handle(UserupdateCommand request, CancellationToken cancellationToken)
        {
            var result=new Reult() { Code=(int)CommonErrorCode.Added};


            try
            {

                var user = await (await _user.GetAll()).Where(x => x.Userid == request.userid).FirstOrDefaultAsync() ;

                if (user!=null)
                {
                    user.USerName=user.USerName;
                    var res = await _user.update(user);
                    if (res != null)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                result.Msg = "修改失败";
                throw;
            }

            return result;
        }
    }
}
